package com.yungam.imcs.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yungam.common.bean.process.ProductProcess;
import com.yungam.common.bean.product.ProductionProduct;
import com.yungam.common.service.ProductProcessService;
import com.yungam.common.service.ProductionProductService;
import com.yungam.common.utils.DateUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/* 产品工具类 */
@Component
public class ProductUtils {

    @Resource
    private DateUtils dateUtils;
    @Resource
    private ProductionProductService productionProductService;
    @Resource
    private ProductProcessService productProcessService;

    /**
     * 获取今日此时已经生产完成的罐子
     */
    public List<ProductionProduct> getFinishedProduct() {

        String[] range = dateUtils.getTodayRange();
        String start = range[0], end = range[1];

        List<ProductionProduct> list = productionProductService.list(
                new QueryWrapper<ProductionProduct>().between("end_time", start, end)
                        .eq("state", 1));

        return list;
    }

    /**
     * 根据日期获取该日生产完成的罐子
     * @param time
     */
    public List<ProductionProduct> getFinishedProductByDate(LocalDateTime time) {

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = df.format(time);

        String start = format + " 00:00:00",
                end = format + " 23:59:59";

        List<ProductionProduct> list = productionProductService.list(
                new QueryWrapper<ProductionProduct>().between("end_time", start, end)
                        .eq("state", 1));

        return list;
    }

    /**
     * 根据产品号获取所有的工序号
     * @param productNo
     */
    public List<ProductProcess> getProcessByProductNo(String productNo) {
        return productProcessService.list(new QueryWrapper<ProductProcess>()
                .eq("product_no", productNo).orderByAsc("process_id"));
    }

}
